<template>
  <div>
    <el-button type="primary" icon="el-icon-message" circle @click="showOrClose"></el-button>
    <el-table v-if="refreshTable"
              :data="tableData"
              style="width: 100%;margin-bottom: 20px;"
              row-key="id"
              :default-expand-all=expand
              border
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="id" label=""></el-table-column>
      <el-table-column prop="path" label="vue组件的访问路径"></el-table-column>
      <el-table-column prop="component" label="vue组件名称"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="iconCls" label="菜单图标名称"></el-table-column>
      <el-table-column prop="parentId" label="父Id"></el-table-column>
      <el-table-column prop="enabled" label="是否可用">
        <template slot-scope="scope">
          <el-switch
              @change="updateStatus(scope.row.id)"
              v-model="scope.row.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-circle-plus-outline"
              circle
              @click="handleSave(scope.$index, scope.row)">
          </el-button>
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleEdit(scope.$index, scope.row)">
          </el-button>
          <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handleDelete(scope.$index, scope.row)">
          </el-button>
        </template>
      </el-table-column>

    </el-table>


    <el-dialog title="添加菜单" :visible.sync="editVisible">
      <el-form :model="form">
        <el-form-item label="菜单">
          <el-input v-model="form.name" autocomplete="off" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path" autocomplete="off" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="组件">
          <el-input v-model="form.component" autocomplete="off" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select v-model="form.iconCls" placeholder="请选择">
            <el-option
                v-for="item in iconClses"
                :key="item.name"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父Id">
          <el-input v-model="form.parentId" autocomplete="off" readonly style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="是否可用">
          <el-switch
              v-model="form.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Menu",
  data() {
    return {
      tableData: [],
      refreshTable: true,
      editVisible: false,
      form: {enabled: true},
      iconClses: [
        {name: 'el-icon-setting'},
        {name: 'el-icon-user-solid'},
        {name: 'el-icon-star-on'},
        {name: 'el-icon-video-camera'},
      ],
      expand: true
    }
  },
  methods: {
    showOrClose() {
      this.refreshTable = false;
      this.expand = !this.expand;
      //  刷新表格结构（不是表数据）
      this.$nextTick(() => {
        this.refreshTable = true;
      })
    },
    getTree() {
      axios.get(this.$baseUrl+'/menu/getTree').then(res => {
        this.tableData = res.data.data;
      })
    },
    handleEdit(index, row) {
      this.editVisible = true;
      this.form = row;
    },
    updateStatus(id) {
      axios.get(this.$baseUrl+'/menu/updateStatus/' + id).then(res => {
        if (res.data.flag) {
          this.getTree()
        }
      })
    },
    handleSave(index, row) {
      this.form = {enabled: true}
      this.editVisible = true;
      this.form.parentId = row.id
    },
    handleDelete(index, row) {
      axios.delete(this.$baseUrl+'/menu/' + row.id).then(res => {
        if (res.data.flag) {
          alert('操作成功')
          // 关闭对话框
          this.getTree()
        }
      })
    },
    save() {
      let result = null;
      if (this.form.id != null) {
        result = axios.put(this.$baseUrl+'/menu', this.form);
      } else {
        result = axios.post(this.$baseUrl+'/menu', this.form);
      }
      result.then(res => {
        if (res.data.flag) {
          alert('操作成功')
          // 关闭对话框
          this.editVisible = false;
          this.getTree()
        } else {
          if (res.data.code === 4001) {
            alert(res.data.msg)
          }
        }
      })
    }

  },
  created() {
    this.getTree();
  }
}
</script>

<style scoped>

</style>